package cn.iocoder.yudao.module.voice.cc.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.zhongweixian.server.websocket.WebSocketServer;

import javax.annotation.Resource;
import java.net.InetSocketAddress;

/**
 * Create by caoliang on 2020/9/20
 */
@Component
public class WebSocketManager {
    private Logger logger = LoggerFactory.getLogger(WebSocketManager.class);

    @Value("${ws.server.port:7250}")
    private Integer port;

    @Value("${ws.server.path:ws}")
    private String path;

    @Resource
    private WebFsSocketHandler webFsSocketHandler;

    private WebSocketServer webSocketServer;

    @Async
    public void start() {
        WsServer wsServer = new WsServer(6000, webFsSocketHandler);
        wsServer.start(new InetSocketAddress(19032));
        webFsSocketHandler.check();
    }
    @Async
    public void stop() {
        if (webSocketServer == null) {
            return;
        }
        logger.info("websocket server:{} stop", port);
        webFsSocketHandler.stop();
        webSocketServer.close();
    }
}
